<?php 
    $url = new Url();
    $link_url = Yii::app()->params['urlRs']."/upload/upload_library.php?forder_up=exercise";  
?>
<link type="text/css" href="<?=Yii::app()->params["urlRs"] ?>/js/jqueryui/jquery-ui-1.8.23.custom.css" rel="stylesheet" />
<script type="text/javascript" src="<?=Yii::app()->params["urlRs"] ?>/js/jqueryui/jquery-ui-1.8.23.custom.min.js"></script>

<link href="<?php echo Yii::app()->params['urlRs']; ?>/js/swfupload/css/default.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript" src="<?php echo Yii::app()->params['urlRs']; ?>/js/swfupload/swfupload.js"></script>
<script type="text/javascript" src="<?php echo Yii::app()->params['urlRs']; ?>/js/swfupload/js/handlers.js"></script>


<div id="main" class="nosidebar">                    
    <div id="content">

        <div class="flash notice" id="flash_notice" style="display: none;">Tạo tài khoản thành công. Click vào <a href="<?php echo $url->createUrl("user/login"); ?>">đây</a> để đăng nhập vào hệ thống!</div>

        <div id="errorExplanation" style="display: none;">
            <ul id="err_msg">
                <!--<li>Email không thể để trắng</li>-->
            </ul>
        </div>
        <div id="register_form">
            <h2>Đăng ký </h2>
            <div class="box tabular fl">
                <div style="width: 600px;float: left;">                
                    <p><label for="user_login">Tài khoản<span class="required"> *</span></label><input id="user_username" name="user[login]" size="25" type="text" value=""></p>
                    <p><label for="user_password">Mật khẩu<span class="required"> *</span></label><input id="user_password" name="user[password]" size="25" type="password">
                        <em class="info">Phải gồm ít nhất 4 ký tự.</em></p>
                    <p><label for="user_password_confirmation">Nhập lại mật khẩu<span class="required"> *</span></label><input id="user_password_confirmation" name="user[password_confirmation]" size="25" type="password"></p>
                    <p><label for="user_fullname">Họ Tên<span class="required"> *</span></label><input id="user_fullname" name="user[firstname]" size="30" type="text" value=""></p>            
                    <p><label for="user_mail">Email<span class="required"> *</span></label><input id="user_email" name="user[mail]" size="30" type="text" value=""></p>
                    <p><label for="user_mail">Ngày sinh</label><input id="user_birthday" name="user[mail]" size="30" type="text" value=""></p>
                    <p><label for="user_mail">Điện thoại</label><input id="user_mobile" name="user[mail]" size="30" type="text" value=""></p>
                    <p><label for="user_mail">Địa chỉ</label><input id="user_address" name="user[mail]" size="30" type="text" value=""></p>
                    <p><label for="user_mail">Chứng minh thư</label><input id="personal_id" name="user[mail]" size="30" type="text" value=""></p>
                    <p><label for="user_mail">Ảnh đại diện</label>                
                        <input type="text" id="txtFileName" disabled="disabled" style="background-color: #FFFFFF;" size="30" value="" />                            
                        <span id="spanButtonPlaceholder"></span>                    
                        <!--<div class="flash" id="divFileProgressContainer" style="display: none;"></div>                -->
                    </p>
                    <div id="divFileProgressContainer" style="margin-left:175px;"></div>           
                </div>

                <div style="float: left;width: 600px;">                     
                    <span id="thumbnails"></span>
                </div>
            </div>
            <div style="clear:both;"></div>
            <input name="commit" type="button" value="Đăng ký" onclick="user_register()">
            <div style="clear:both;"></div>
        </div>
    </div>
</div>
<script type="">

    //-----------------------------------------------------------------------------------------------------------------//   

    var filename = "";
    var filepath = "";
    function getdata(data_json){              
        var json = $.parseJSON(data_json);
        var code = json.code;   

        if(code == '105')
            {
            filename = json.data["filename"];            
            //filetype = json.data["filetype"];
            //filesize = json.data["filesize"];
            filepath = "/" + json.data["forder_up"]  + "/" + ""+filename;
            path = "<?=Yii::app()->params['urlImage']?>/"+ json.data["forder_up"]  + "/" + ""+filename;            
            //path = "http://images.az24.vn"+ json.data["path"]  + "/" + ""+filename;            
            $("#txtFileName").val(filepath);
            $("#thumbnails").html("<img style='max-height: 400px;max-width: 400px;' src='"+path+"'/>");                          
        }
        else
            {
            var msg = json.msg;                
            alert(msg);    
        }
    }

    //-----------------------------------------------------------------------------------------------------------------//

    var swfu;
    window.onload = function () {
        swfu = new SWFUpload({
            // Backend Settings
            upload_url: "<?=$link_url ?>",
            post_params: "resume_file",

            // File Upload Settings
            file_size_limit : "4 MB",
            file_types : "*.*",
            file_types_description : "Tài liệu",
            file_upload_limit : "0",

            file_queue_error_handler : fileQueueError,
            file_dialog_complete_handler : fileDialogComplete,
            upload_progress_handler : uploadProgress,
            upload_error_handler : uploadError,
            upload_success_handler : uploadSuccess,
            upload_complete_handler : uploadComplete,

            // Button Settings
            button_image_url : "<?php echo Yii::app()->params['urlRs']; ?>/js/swfupload/XPButtonUploadText_61x22.png",
            button_placeholder_id : "spanButtonPlaceholder",
            button_width: 61,
            button_height: 22,

            // Flash Settings
            flash_url : "<?php echo Yii::app()->params['urlRs']; ?>/js/swfupload/swfupload.swf",

            custom_settings : {
                upload_target : "divFileProgressContainer"
            },

            // Debug Settings
            //debug: true
        });
    };

    //-----------------------------------------------------------------------------------------------------------------//    

    $(function() {
        $('#user_birthday').datepicker({
            changeYear: true,
            dateFormat:'dd/mm/yy',                                    
            showOn:'both',           
            buttonImage: '<?=Yii::app()->params["urlRs"]."/images/calendar.png"?>',
            buttonImageOnly: 'true'
        });   

        $(".ui-datepicker-trigger").css("margin-bottom","-5px");
        $(".ui-datepicker-trigger").css("margin-left","5px"); 
    });

    function user_register(){        
        var user_username = $('#user_username').val();       
        var user_password = $('#user_password').val();       
        var user_password_confirmation = $('#user_password_confirmation').val();       
        var user_fullname = $('#user_fullname').val();       
        var user_mail = $('#user_email').val();       
        var user_birthday = $('#user_birthday').val();       
        var user_mobile = $('#user_mobile').val();       
        var user_address = $('#user_address').val();       
        var personal_id = $('#personal_id').val();       
        var flag_err = 0;
        var err_msg = '';
        $('#errorExplanation').slideUp('slow');
        $('#err_msg').empty();        
        if(user_username == ''){
            flag_err = 1;
            err_msg = '<li>Chưa điền tên tài khoản!</li>'
            $('#err_msg').append(err_msg);
        }
        if(user_password == ''){
            flag_err = 1;
            err_msg = '<li>Chưa điền mật khẩu!</li>'
            $('#err_msg').append(err_msg);
        }
        if(user_password.length < 3 || user_password.length > 20){
            flag_err = 1;
            err_msg = '<li>Mật khẩu chỉ được trong khoảng từ 3 đến 20 ký tự!</li>'
            $('#err_msg').append(err_msg);
        }
        if(user_password != user_password_confirmation){
            flag_err = 1;
            err_msg = '<li>Xác nhận mật khẩu sai!</li>'
            $('#err_msg').append(err_msg);
        }
        if(user_fullname == ''){
            flag_err = 1;
            err_msg = '<li>Chưa điền tên!</li>'
            $('#err_msg').append(err_msg);
        }
        if(user_mail == ''){
            flag_err = 1;
            err_msg = '<li>Chưa điền mail!</li>'
            $('#err_msg').append(err_msg);
        }
        if(flag_err == 1){
            $('#errorExplanation').slideDown('slow');
            return false;    
        }else{            
            $('#errorExplanation').slideUp('slow');
            var strUrl = '<?=$url->createUrl("user/ajaxSubmitRegister") ?>';            
            loadingAjax();
            $.ajax({
                type: "GET",
                url: strUrl,
                data: {user_username:user_username,user_password:user_password,user_fullname:user_fullname,user_mail:user_mail,user_birthday:user_birthday,user_mobile:user_mobile,user_address:user_address,filepath:filepath,personal_id:personal_id},
                success: function(msg){                            
                    if(msg==1){                       
                        $('#errorExplanation').slideUp('slow');
                        $('#flash_notice').slideDown('slow');
                        $('#register_form').slideUp('slow');
                    }else if(msg==-3){
                        err_msg = '<li>Tài khoản đã tồn tại! Hãy nhập tài khoản khác!</li>'
                        $('#err_msg').append(err_msg);   
                        $('#errorExplanation').slideDown('slow');                     
                    }

                }
            });
        }  
        closeLoadingAjax();          
    }

</script>